// pages/my-order-detail/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderId: "",
    order: {},
    type: "",
  },

  getOrderDetail() {
    const { orderId } = this.data;

    const params = {
      orderId,
    };

    wx.ajax.post("queryOrderById.action", params, true).then((res) => {
      // console.log(res);
      if (res.success) {
        this.setData({
          order: res.result,
        });
      }
    });
  },

  handlePay() {
    const { orderId } = this.data;

    const that = this;

    const params = { orderId };

    wx.ajax.post("orderRepayment.action", params, true).then((res) => {
      // console.log(res);

      if (res.success) {
        wx.showLoading({
          title: "付款中",
          mask: true,
        });

        const wxPayParam = res.result.wxPayParam;

        wx.requestPayment({
          timeStamp: wxPayParam.timeStamp,
          nonceStr: wxPayParam.nonceStr,
          package: wxPayParam.packageStr,
          signType: wxPayParam.signType,
          paySign: wxPayParam.sign,
          success: function () {
            wx.hideLoading();

            wx.navigateBack({
              delta: 1,
            });
          },
          fail: function () {
            wx.hideLoading();

            wx.showToast({
              title: "稍后再试",
              icon: "none",
              duration: 1000,
            });
          },
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000,
        });
      }
    });
  },

  handleCancelOrder() {
    wx.showModal({
      title: "是否确认",
      content: "确认取消该订单?",
      success: (res) => {
        console.log(res);
        if (res.confirm) {
          this.handleSureCancelOrder();
        }
      },
    });
  },

  handleSureCancelOrder() {
    const { orderId, type } = this.data;
    const params = { orderId };

    wx.ajax
      .post(
        type == "7" ? "userCancelOrder.action" : "merchantCancelOrder.action",
        params,
        true
      )
      .then((res) => {
        if (res.success) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });

          const timer = setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            });

            clearTimeout(timer);
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000,
          });
        }
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { id, type } = options;

    this.setData({
      orderId: id,
      type,
    });

    this.getOrderDetail();
  },

  handleConfirmOrder() {
    wx.showModal({
      title: "是否确认",
      content: "确认收货？",
      success: (res) => {
        if (res.confirm) {
          this.handleConfirmGet();
        }
      },
    });
  },

  handleOfflinePayOrder() {
    wx.showModal({
      title: "是否确认",
      content: "确认线下收款？",
      success: (res) => {
        if (res.confirm) {
          this.handleOfflinePay();
        }
      },
    });
  },

  handleConfirmGet() {
    const { orderId } = this.data;
    const params = { orderId };
    wx.ajax.post("confirmGet.action", params, true).then((res) => {
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });

        const timer = setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          });

          clearTimeout(timer);
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });
      }
    });
  },

  handleOfflinePay() {
    const { orderId } = this.data;
    const params = { orderId };
    wx.ajax.post("offlinePay.action", params, true).then((res) => {
      if (res.success) {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });

        const timer = setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          });

          clearTimeout(timer);
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
